/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.expert.dao.SolutionFaceProductStepDao;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductStep;

/**
 *  方案产品步骤Service
 * @author liujiang
 * @version 2013-08-19
 */
@Component
@Transactional(readOnly = true)
public class SolutionFaceProductStepService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SolutionFaceProductStepService.class);
	
	@Autowired
	private SolutionFaceProductStepDao solutionFaceProductStepDao;
	
	public SolutionFaceProductStep get(Long id) {
		return solutionFaceProductStepDao.findOne(id);
	}
	
	public Page<SolutionFaceProductStep> find(Page<SolutionFaceProductStep> page, SolutionFaceProductStep solutionFaceProductStep, Long solutionId) {
		DetachedCriteria dc = solutionFaceProductStepDao.createDetachedCriteria();
		if (solutionId != null) {
			dc.add(Restrictions.eq("solutionFace.id", solutionId));
		}
		dc.addOrder(Order.asc("step"));
		return solutionFaceProductStepDao.find(page, dc);
	}
	
	public int check(Long solutionId, int step) {
		DetachedCriteria dc = solutionFaceProductStepDao.createDetachedCriteria();
		dc.add(Restrictions.eq("solutionFace.id", solutionId));
		dc.add(Restrictions.eq("step", step));
		return (int) solutionFaceProductStepDao.count(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(SolutionFaceProductStep solutionFaceProductStep) {
		solutionFaceProductStepDao.save(solutionFaceProductStep);
	}
	
	@Transactional(readOnly = false)
	public void delete(SolutionFaceProductStep solutionFaceProductStep) {
		solutionFaceProductStepDao.delete(solutionFaceProductStep);
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId, Long solutionId) {
		DetachedCriteria dc = solutionFaceProductStepDao.createDetachedCriteria();
		dc.add(Restrictions.le("id", currentId));
		if(solutionId !=null) {
			dc.add(Restrictions.eq("solutionFace.id", solutionId));
		}
		dc.addOrder(Order.asc("step"));
		return (int)solutionFaceProductStepDao.count(dc);
	}
}
